import axios from 'axios'

/**
 * axios 全局配置
 */
axios.defaults.baseURL = 'http://www.nlelearn.com/spider'
axios.defaults.headers.common['Authorization'] = 'AUTH_TOKEN'
axios.defaults.headers.post['Content-Type'] = 'application/json'

const spiderUrl = 'http://127.0.0.1:6800'

// 验证用户的登录
export const checkLoginUser = (params) => { return axios.post('/checkUser/', params) }
/**
 * 用户管理
 */
export const getUserlist = (page) => { return axios.get('/myUser/?format=json&page=' + page) }  // 分页获取
export const createUser = (params) => { return axios.post('/myUser/', params) } // 新增
export const putUser = (url, params) => { return axios.put(url, params) } // 更新

// 分页获取tasks
// export const gainPageTasks = (page) => { return axios.get('/tasks/?format=json&page=' + page) }

// 获取tasks
export const gainTasks = (page) => {
  if (page === 0) {
    return axios.get('/tasks/?format=json')
  } else {
    return axios.get('/tasks/?format=json&page=' + page)
  }
}

// 创建tasks
export const createTasks = (params) => { return axios.post('/tasks/', params) }

// 检查tasks
export const checkTasks = () => { return axios.get('/checkTask/?format=json') }

// 获取tasklist
export const getTaskList = (page) => { return axios.get('/taskList/?format=json&page=' + page) }

// 生成TaskList记录
export const postTaskList = (params) => { return axios.post('/taskList/', params) }

// postSpiderUrl
export const postSpider = (params) => { return axios.post(spiderUrl + '/schedule.json', params, {emulateJSON: true}) }

// 删除
export const deleteAny = (url) => { return axios.delete(url) }

// 爬取58
export const getExcelList = () => { return axios.get('/getExcelList/') }

export const postHouseSource = (params) => { return axios.post('/getHouseMsg/', params) }

export const getHousePrice = (name) => { return axios.get('/getHouseMsg/?name=' + name) }

export const getAvgPrice = (city, style) => { return axios.get('/getPrice/?city=' + city + '&style=' + style) }

export const getPriceToMap = (style) => { return axios.get('/getPriceToMap/?style=' + style) }

export const getForcecastHousePrice = () => { return axios.get('/forcecastHousePrice/') }

export const postArea = (params) => { return axios.post('/forcecastHousePrice/', params) }

export const timeFormat = (time) => {
  let d = new Date(time)
  return d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes()
}
